﻿using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.Painting.Effects;
using MeterReader.Common.Config;
using SkiaSharp;
using System.Collections.ObjectModel;

namespace MeterReader.Models
{
    public partial class MeterDataCollection : ObservableObject
    {
        private int _maxCount = GlobalSettings.MaxMaxSeriesPonts;

        /// <summary>
        /// 电能表测量值集合
        /// </summary>
        public ObservableCollection<MeterData>? Collection { get; set; }

        /// <summary>
        /// 电能表最新的测量值
        /// </summary>
        public MeterData MeterLatestData => Collection!.LastOrDefault()!;

        /// <summary>
        /// 曲线绑定值
        /// </summary>
        public ObservableCollection<ISeries>? Series { get; set; }

        /// <summary>
        /// 曲线 X 轴
        /// </summary>
        public Axis[] XAxes { get; set; }

        /// <summary>
        /// 曲线 Y 轴
        /// </summary>
        public Axis[] YAxes { get; set; }

        /// <summary>
        /// 集合添加方法
        /// </summary>
        public void AddToCollection(MeterData meterData)
        {
            Collection!.Add(meterData);
            if (Collection.Count > _maxCount)
            {
                Collection.RemoveAt(0);
            }
        }

        public MeterDataCollection()
        {
            Collection = new();
            Collection.CollectionChanged += Meters_CollectionChanged;
            // 曲线定义
            Series = new()
            {
                new LineSeries<MeterData>
                {
                    Values = Collection,
                    Fill = null,
                    GeometrySize = 0,
                    LineSmoothness = 1,
                    Mapping = (Collection, index) => new(index, Collection.MeterValue!.Value),
                    //IsHoverable = false,
                    //EasingFunction = null,
                }
            };
            XAxes =
            [
                new Axis()
                {
                    //Name = "次数",
                    NameTextSize = 15,
                    NamePaint = new SolidColorPaint(SKColors.WhiteSmoke),
                    LabelsPaint = new SolidColorPaint(SKColors.DarkGray),
                    TextSize = 15,
                    SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray) { StrokeThickness = 2 },
                    MinStep = 1,
                    //CrosshairLabelsBackground = SKColors.DarkOrange.AsLvcColor(),
                    //CrosshairLabelsPaint = new SolidColorPaint(SKColors.DarkRed, 1),
                    //CrosshairPaint = new SolidColorPaint(SKColors.DarkOrange, 1),
                    //Labeler = v => v.ToString("N2")
                }
            ];
            YAxes =
            [
                new Axis()
                {
                    //Name = "总功率(Kw.h)",
                    NameTextSize = 15,
                    NamePaint = new SolidColorPaint(SKColors.WhiteSmoke),
                    LabelsPaint = new SolidColorPaint(SKColors.DarkGray),
                    TextSize = 15,
                    SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray)
                    {
                        StrokeThickness = 2,
                        PathEffect = new DashEffect(new float[] { 3, 3 })
                    },
                    //CrosshairLabelsBackground = SKColors.DarkOrange.AsLvcColor(),
                    //CrosshairLabelsPaint = new SolidColorPaint(SKColors.DarkRed, 1),
                    //CrosshairPaint = new SolidColorPaint(SKColors.DarkOrange, 1),
                    //Labeler = v => v.ToString("N2"),
                    //CrosshairSnapEnabled = true
                }
            ];
            LiveCharts.Configure(config => config.HasGlobalSKTypeface(SKFontManager.Default.MatchCharacter('汉')));
        }

        private void Meters_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            OnPropertyChanged(nameof(MeterLatestData));
        }
    }
}